package www.tvbigo.cn.xiaoxuan.activity;

/******************************************************
 *
 ***********本工程的源于www.tvbigo.cn开发社区***************
 *************我们的开发QQ交流群：257251953****************
 ****我们的宗旨是服务广大Android/TVOS开发者 欢迎大家加入我们******
 ***本源码仅用于学习 如有商用请联系我们 一经发现我们有追究其责任权利***
 *
 ******************************************************/

import www.tvbigo.cn.xiaoxuan.R;
import www.tvbigo.cn.xiaoxuan.app.APPActivity;
import www.tvbigo.cn.xiaoxuan.widget.FlyBorderView;
import www.tvbigo.cn.xiaoxuan.widget.MainLayout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;

/******************************************************
 * @Title: HomeActivity.java
 * @Copyright: XXX Co., Ltd. Copyright YYYY-YYYY, All rights reserved
 * @Description: TODO <Home 页>
 * @author: Joychang
 * @data: 2016-1-11 上午10:35:18
 * @version: V1.0
 ******************************************************/
public class HomeActivity extends Activity implements OnClickListener, OnFocusChangeListener
{
    private static final String TAG = HomeActivity.class.getSimpleName();
    
    private ImageView[] ticketbgs;
    
    private Integer[] ivs;
    
    private FlyBorderView whiteBorder;
    
    private MainLayout ll_home;
    
    private FrameLayout fl_main;
    
    private Context mContext;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        mContext = this;
        initView();
    }
    
    private void initView()
    {
        loadViewLayout();
        findViewById();
        setListener();
    }
    
    protected void loadViewLayout()
    {
        ticketbgs = new ImageView[9];
        ivs = new Integer[9];
    }
    
    protected void findViewById()
    {
        fl_main = (FrameLayout)findViewById(R.id.fl_main);
        ll_home = (MainLayout)findViewById(R.id.ll_home);
        ivs =
            new Integer[] {R.drawable.img_blue, R.drawable.img_green, R.drawable.img_jasper, R.drawable.img_red,
                R.drawable.img_yellow, R.drawable.img_blue, R.drawable.img_yellow, R.drawable.img_green,
                R.drawable.img_red};
        ticketbgs[0] = (ImageView)findViewById(R.id.iv_home_0);
        ticketbgs[1] = (ImageView)findViewById(R.id.iv_home_1);
        ticketbgs[2] = (ImageView)findViewById(R.id.iv_home_2);
        ticketbgs[3] = (ImageView)findViewById(R.id.iv_home_3);
        ticketbgs[4] = (ImageView)findViewById(R.id.iv_home_4);
        ticketbgs[5] = (ImageView)findViewById(R.id.iv_home_5);
        ticketbgs[6] = (ImageView)findViewById(R.id.iv_home_6);
        ticketbgs[7] = (ImageView)findViewById(R.id.iv_home_7);
        ticketbgs[8] = (ImageView)findViewById(R.id.iv_home_8);
        
        for (int i = 0; i < ticketbgs.length; i++)
        {
            ticketbgs[i].setOnFocusChangeListener(this);
            ticketbgs[i].setOnClickListener(this);
            int imageId = ivs[i];
            ticketbgs[i].setImageResource(imageId);
        }
        initwhiteBorder();
    }
    
    protected void setListener()
    {
        ll_home.getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener()
        {
            @Override
            public void onGlobalFocusChanged(View oldFocus, View newFocus)
            {
                whiteBorder.startTranslateAnimation(newFocus, 1.0f, 1.0f);
            }
        });
    }
    
    public View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener()
    {
        
        @Override
        public void onFocusChange(View v, boolean hasFocus)
        {
            
            int focus = 0;
            if (hasFocus)
            {
                focus = R.anim.anim_scale_big;
            }
            else
            {
                focus = R.anim.anim_scale_small;
            }
            // 如果有焦点就放大，没有焦点就缩小
            Animation mAnimation = AnimationUtils.loadAnimation(mContext, focus);
            mAnimation.setBackgroundColor(Color.TRANSPARENT);
            mAnimation.setFillAfter(hasFocus);
            v.startAnimation(mAnimation);
            mAnimation.start();
            v.bringToFront();
        }
    };
    
    /**
     * 初始化飞框
     */
    public void initwhiteBorder()
    {
        Log.d(TAG, "开始whiteBorder=" + whiteBorder);
        this.whiteBorder = new FlyBorderView(this);
        FrameLayout.LayoutParams layoutparams =
            new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        whiteBorder.setLayoutParams(layoutparams);
        whiteBorder.setBorderPadding(getResources().getDimensionPixelOffset(R.dimen.sm_230),
            getResources().getDimensionPixelOffset(R.dimen.sm_100),
            getResources().getDimensionPixelOffset(R.dimen.sm_230),
            getResources().getDimensionPixelOffset(R.dimen.sm_100));
        fl_main.addView(whiteBorder);
        whiteBorder.setVisibility(View.INVISIBLE);
    }
    
    @Override
    public void onFocusChange(View v, boolean hasFocus)
    {
        // ...
    }
    
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_home_8:
                Intent intent = new Intent(HomeActivity.this, APPActivity.class);
                startActivity(intent);
                break;
            
            default:
                break;
        }
    }
}
